﻿#include "examinterface.h"
#include <QApplication>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QDebug>
#include <QLabel>
#include <QRadioButton>
#include <QCheckBox>
#include <QPushButton>

typedef  enum class  STATU
{
    BEGIN,
    QUESTION,
    ITEMS,
    ANSWER
}STATU;

ExamInterface::ExamInterface(QWidget*parent):QDialog(parent),timeGo(0),lineMaxQNum(10)
{
    setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint);
    resize(800,900);
    setPalette(QColor(255,200,33));
    QFont font;
    font.setPointSize(12);
    setFont(font);
    setWindowTitle("exam time: 0分0秒");


    initTimer();
    initLayout();
    if(!initText())
    {
        //qApp->quit();
        QTimer::singleShot(0,qApp,SLOT(quit()));//退出
        return;
    }
    initButtons();
    show();
}

void ExamInterface::initTimer()
{
    timer=new QTimer;
    timer->setInterval(1000);
    timer->start();
    connect(timer,SIGNAL(timeout()),this,SLOT(updateTime()));
}

void ExamInterface::initLayout()
{
    layout=new QGridLayout(this);
    layout->setSpacing(10);
    layout->setMargin(10);
}

bool ExamInterface::initText()
{
    QTextEdit*textEdit=new QTextEdit;
    textEdit->setReadOnly(true);

    QString filename("../exam.txt");
    QFile file(filename);
    QTextStream textStream(&file);
    textStream.setCodec("UTF-8");
    if(file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        QString strLine;
        QString strLine2;
        QString strText;
        bool first=false;
        STATU statu=STATU::BEGIN;
        bool needRead=true;

        QString label;
        QStringList items;
        QStringList answers;

        while(1)
        {
            if(needRead)
            {
                if(textStream.atEnd())break;
                strLine=textStream.readLine();
                strLine=strLine.trimmed();
                if(strLine.size()<1)continue;
                if(strLine.at(0)=='#')continue;
                strLine2=strLine;
                qDebug()<<"行:"<<strLine2;
            }
            else
            {
                needRead=true;
            }



            switch (statu)
            {
                case STATU::BEGIN:
                                items.clear();
                                answers.clear();
                                if(strLine2.at(0)=='Q')
                                {
                                    needRead=false;
                                    statu=STATU::QUESTION;
                                }
                                else
                                {
                                    QMessageBox::information(this,"提示","未读到题目!");
                                    return false;
                                }
                                break;
                case STATU::QUESTION:
                                if(strLine2.at(0)=='Q')
                                {
                                    strLine2.remove(0,1);
                                    int pos=strLine2.indexOf('.');
                                    strLine2.resize(pos);
                                    label=strLine2;
                                    qDebug()<<"标签:"<<label;
                                    statu=STATU::ITEMS;
                                }
                                else
                                {
                                    QMessageBox::information(this,"提示","未读到题目!");
                                    return false;
                                }
                                break;
                case STATU::ITEMS:
                                if(strLine2.at(0)=='.')
                                {
                                    strLine2.remove(0,1);
                                    int pos=strLine2.indexOf(' ');
                                    if(pos>=1)
                                    {
                                        strLine2.resize(pos);
                                        qDebug()<<"选项:"<<strLine2;
                                        items.append(strLine2);

                                    }
                                    else
                                    {
                                        qDebug()<<"选项:"<<strLine2;
                                        items.append(strLine2);
                                    }
                                    statu=STATU::ITEMS;
                                }
                                else if(strLine2.at(0)=='!')
                                {
                                    needRead=false;
                                    statu=STATU::ANSWER;
                                }
                                else
                                {
                                    QMessageBox::information(this,"提示","第"+label+"题 选项读取错误!");
                                    return false;
                                }
                                break;
                case STATU::ANSWER:
                                if(strLine2.at(0)=='!')
                                {
                                    strLine2.remove(0,1);
                                    if((strLine2[0]>'a'&&strLine2[0]<'z')||(strLine2[0]>'A'&&strLine2[0]<'Z'))
                                    {
                                        for(int i=0;i<strLine2.size();i++)
                                        {
                                            qDebug()<<"答案:"<<strLine2[i];
                                            answers.append(QString(strLine2[i]));
                                        }
                                    }
                                    else
                                    {
                                        qDebug()<<"答案:"<<strLine2;
                                        answers.append(strLine2);
                                    }

                                    AnswerItem*answerItem=new AnswerItem(label,answers);
                                    for(int i=0;i<items.size();i++)
                                    {
                                        answerItem->setItems(items[i]);
                                    }


                                    answerItems.append(answerItem);
//                                    static int count=0;
//                                    count++;
//                                    qDebug()<<count;

                                    statu=STATU::BEGIN;
                                }
                                else
                                {
                                    QMessageBox::information(this,"提示","第"+label+"题 未设置答案选项!");
                                    return false;
                                }
                                break;
                default:
                                break;
            }

            if(strLine.at(0)=='Q')
            {
                if(first)
                {
                    strText.append("\n");
                }
                else
                {
                    first=true;
                }
                strText.append(strLine.remove(0,1));
                strText.append("\n");
            }
            if(strLine.at(0)=='.')
            {
                strText.append(strLine.remove(0,1));
                strText.append("\n");
            }


        }
        textEdit->setText(strText);
        layout->addWidget(textEdit,0,0,1,10);
    }
    else
    {
        QMessageBox::information(this,"提示","文件打开失败!");
        file.close();
        return false;
    }

    file.close();
    return true;
}

void ExamInterface::initButtons()
{

    int lineNo=1;
    int maxLine=0;
    //qDebug()<<answerItems.size();
    for(int i=0;i<answerItems.size();i++)
    {
        QLabel *label=new QLabel;
        label->setText("第"+answerItems[i]->getqLabel()+"题");
        if(i%lineMaxQNum==0&&i!=0)
        {
            lineNo+=maxLine+1;
            maxLine=0;
        }
        if(maxLine<answerItems[i]->getNum())
        {
            maxLine=answerItems[i]->getNum();
        }
        layout->addWidget(label,lineNo,i%lineMaxQNum);
        QButtonGroup* bg=new QButtonGroup;
        if(answerItems[i]->getIsSignal())
        {
            for(int j=0;j<answerItems[i]->getNum();j++)
            {
                QRadioButton*rb=new QRadioButton;
                rb->setText(answerItems[i]->getItems()[j]);
                bg->addButton(rb);
                layout->addWidget(rb,lineNo+1+j,i%lineMaxQNum);
            }
        }
        else
        {
            bg->setExclusive(false);
            for(int j=0;j<answerItems[i]->getNum();j++)
            {
                QCheckBox*cb=new QCheckBox;
                cb->setText(answerItems[i]->getItems()[j]);
                bg->addButton(cb);
                layout->addWidget(cb,lineNo+1+j,i%lineMaxQNum);
            }
        }
        BtnGroup.append(bg);
    }

    QPushButton *pushBtn=new QPushButton;
    pushBtn->setFixedSize(60,40);
    pushBtn->setText("提交");
    layout->addWidget(pushBtn,lineNo+maxLine+1,lineMaxQNum-1);
    connect(pushBtn,SIGNAL(clicked()),this,SLOT(getScore()));

}

void ExamInterface::updateTime()
{
    timeGo++;
    QString timeStr="exam time: "+QString::number(timeGo/60)+"分"+QString::number(timeGo%60)+"秒";
    setWindowTitle(timeStr);
}

void ExamInterface::getScore()
{
    int score=10;
    int totalScore=0;

    for(int i=0;i<answerItems.size();i++)
    {
        if(!BtnGroup[i]->checkedButton())
        {
            QMessageBox::information(this,"提示","还有选项未作答,请作答!");
            return;
        }
        if(answerItems[i]->getIsSignal())
        {
            if(BtnGroup[i]->checkedButton()->text()==answerItems[i]->getAnswers()[0])
            {
                totalScore+=score;
            }
        }
        else
        {
            int checkeds=0;
            int validItem=0;
            QList<QAbstractButton *> btnlist=BtnGroup[i]->buttons();
            for(int j=0;j<btnlist.size();j++)
            {
                if(btnlist[j]->isChecked())
                {
                    checkeds++;
                    for(int k=0;k<answerItems[i]->getAnswers().size();k++)
                    {
                        if(btnlist[j]->text()==answerItems[i]->getAnswers()[k])
                        {
                            validItem++;
                        }
                    }
                }
            }
            if(checkeds==validItem&&validItem==answerItems[i]->getAnswers().size())
            {
                totalScore+=score;
            }
        }
    }

    int res=QMessageBox::information(this,"提示","总分为: "+QString::number(totalScore)+"是否重新考试?",QMessageBox::Yes|QMessageBox::No);

    if(res==QMessageBox::Yes)
    {
        return;
    }
    else
    {
        close();
    }
}
